Player = {}
local this = Player

local transform
local gameObject 

local flySpeed = 20
local bulletSpeed = 20
local maxX
local maxY

local fireRate = 4
local fireInterval = 0
local fireTime = 0

local firePoint

local score = 0
local scoretext = GameObject.Find("Score"):GetComponent(typeof(Text))

local hp = 10
local hptext = GameObject.Find("hp"):GetComponent(typeof(Text))

local boom
local player
local Canvas
local OverPanel
local over = false

function Player.New()

    Canvas = GameObject.Find("UIRoot/Canvas").transform 

    local screenMax = Camera.main:ScreenToWorldPoint(Vector3(Screen.width,Screen.height))
    maxX = screenMax.x
    maxY = screenMax.y

    fireInterval = 1/fireRate
    fireTime = Time.time

    gameObject = newObject(ThunderManager.prefabs[1])
    transform = gameObject.transform

    boom = transform:Find("boom")
    player = transform:Find("Player")
    firePoint = transform:Find("Player/FirePoint")

    local luaTrigger = gameObject:AddComponent(typeof(LuaRigidTrigger))
    luaTrigger:AddHandler(this.OnTriggerEnter2D)

    UpdateBeat:Add(this.Update)
    UpdateBeat:Add(this.OnOverClick)

end

function Player.Update(  )
    this.Move()
    this.CheckFire()
end

function Player.Move( )
    if (Input.GetMouseButton(0)) then
        local pos = Camera.main:ScreenToWorldPoint(Input.mousePosition)
        pos.z = 0 
        pos.x = Mathf.Clamp(pos.x,-maxX,maxX)
        pos.y = Mathf.Clamp(pos.y,-maxY,maxY)

        local vec = pos - transform.position
        if (Vector3.SqrMagnitude(vec)<0.5) then
            transform.position = pos
        else
            local dir = Vector3.Normalize(vec)
            transform:Translate(dir * flySpeed * Time.deltaTime)
        end
    end
end

function Player.CheckFire()
    if (Time.time-fireTime>fireInterval) then
        fireTime=Time.time

        this.Fire()
    end
end

function Player.Fire()
    local bullet = Bullet.new(firePoint,bulletSpeed)
end

function Player.OnTriggerEnter2D( collider)
    if (collider.gameObject:CompareTag("EnemyBullet")) then
        Player.hp()
        destroy(collider.gameObject)      
        log("被敌机击中")
    end
end

function Player.GameOver()
    OverPanel = newObject(ThunderManager.prefabs[12])
    OverPanel.transform:SetParent(Canvas,false)
    over = true
end

function Player.OnOverClick()
   if  over == true then
        if (Input.GetKeyDown(KeyCode.X)) then
            log("========================")

            destroy(OverPanel.gameObject)

            hp = 10
            score = 0 
            over = false

            --初始化玩家
            Player.New()
        end
   end
   
end

function Player.Score()
    score = score + 1 
    scoretext.text = "Score:".. score
end

function Player.hp()  
    hp = hp - 1
    hptext.text = "HP:"..hp
    if (hp==0) then  
        UpdateBeat:Remove(this.Update)
        boom.gameObject:SetActive(true)
        player.gameObject:SetActive(false)
        GameObject.Destroy(gameObject,1) 
        this.GameOver()   
    end
end